package com.goldze.mvvmhabit.ui.tab_bar.fragment.leave;

import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.view.View;

import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;


public class LeaveItemViewModel extends ItemViewModel<LeaveViewModel> {

    public ObservableField<String> entity = new ObservableField<>();

    public LeaveItemViewModel(@NonNull LeaveViewModel viewModel, String entity) {
        super(viewModel);
        this.entity.set(entity);
    }

    /**
     * 获取position的方式有很多种,indexOf是其中一种，常见的还有在Adapter中、ItemBinding.of回调里
     *
     * @return
     */
    public int getPosition() {
        return viewModel.getItemPosition(this);
    }

    public String getImageUrl() {
        return entity.get();
    }

    public int isVisible() {
        if (viewModel.observableList.get(getPosition()).entity.get() == null) {
            return View.GONE;
        } else {
            return View.VISIBLE;
        }
    }

    public int isAddVisible() {
        if (viewModel.observableList.get(getPosition()).entity.get() == null) {
            return View.VISIBLE;
        } else {
            return View.GONE;
        }
    }

    public BindingCommand add_data = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            viewModel.showAlbum();
        }
    });

    public BindingCommand delete_data = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            viewModel.observableList.remove(getPosition());
            for (LeaveItemViewModel item : viewModel.observableList) {
                if(item.entity.get() == null){
                    return;
                }
            }
            viewModel.addDefaultLastData();
        }
    });

    public BindingCommand show_iamge = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

        }
    });
}
